﻿using NetServer;
using System.Net.Sockets;

namespace DeviceController.NetCoreServer.Services
{
    public class ErrorEventArgs : EventArgs
    {
        public SocketError SocketError { get; }

        public ErrorEventArgs(SocketError socketError)
        {
            SocketError = socketError;
        }
    }

    public class DeviceControllerSession : TcpSession
    {
        public event EventHandler<ReceivedEventArgs>? Received;

        public event EventHandler<ErrorEventArgs>? Error;

        public DeviceControllerSession(TcpServer server) : base(server)
        {
        }

        protected override void OnReceived(byte[] buffer, long offset, long size)
        {
            Received?.Invoke(this, new ReceivedEventArgs(buffer.Skip((int)offset).Take((int)size).ToArray()));
        }

        protected override void OnError(SocketError error)
        {
            Error?.Invoke(this, new ErrorEventArgs(error));
        }
    }
}
